//
//  lishiViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/24.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class lishiViewController: UIViewController {
    var informationRecordId : String!
    
       var tableView  : UITableView!
    
        var lishiArray = [lishiModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = bgGrayColor;
        
        navigationItem.title="历史问题"
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = UIView.init()
        
        
        tableView.register(NSClassFromString("lishiTableViewCell"), forCellReuseIdentifier: "lishiTableViewCell")
        
        
        self.view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        self.loadDate()

        // Do any additional setup after loading the view.
    }
    func loadDate()  {
        
        
        let manager = UserDefaults()
        
        
        let token: String? = manager.string(forKey:"tokens")  //根据关键字取值
        
        
        let dubanurlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/questionForm/history"

        let dubanjson = "{\"informationRecordId\":\"\(self.informationRecordId!)\",\"start\":\"\(0)\",\"pageSize\":\"\(5)\",\"token\":\"\(token!)\"}"
        
        
        let dubanurl = URL(string: dubanurlString)!
        let dubanjsonData = dubanjson.data(using: .utf8, allowLossyConversion: false)!
        
        var dubanrequest = URLRequest(url: dubanurl)
        dubanrequest.httpMethod = HTTPMethod.post.rawValue
        dubanrequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
        dubanrequest.httpBody = dubanjsonData
        
        Alamofire.request(dubanrequest).responseJSON {(response) in
            print("1111====\(response)")
     
                
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    let model1 =   lishiModel()
                    
                    
                    model1.createtime = dataDic["createtime"].string
                    
                    model1.departmentName = dataDic["departmentName"].string
                    
                    model1.questionDesp = dataDic["questionDesp"].string
                    
                    model1.questionFormId = dataDic["questionFormId"].string
                    
                    model1.questionLevel = dataDic["questionLevel"].string
                    
                    self.lishiArray.append(model1)
                    
                    
                }
                
                self.tableView.reloadData()
            }
        }
    }



}
extension lishiViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.lishiArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 110
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let lishimodel:lishiModel = self.lishiArray[indexPath.row]
        
        let cell = lishiTableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "lishiTableViewCell")
        
        
        cell.lishiWithModel(model: lishimodel)
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
      
        
    }
    
    
}
